import tempfile, shutil

class TemporaryDirectory:
    def __init__(self):
        self.path = tempfile.mkdtemp()

    def __del__(self):
        self.close()

    def close(self):
        if self.path != '':
            try:
                shutil.rmtree(self.path)
            except OSError, e:
                # Ignore code 2: no such file or directory.
                if e.errno != 2:
                    raise
            self.path = ''

    @property
    def path(self):
        return self.path
